import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
var _excluded = ["page"];

/**
 * model - 语言维护
 * @date: 2018-8-9
 */
import { getResponse, createPagination } from 'utils/utils';
import { queryLanguage, editLanguage as _editLanguage } from "../services/languagesService";
export default {
  namespace: 'languages',
  state: {
    query: {},
    // 查询参数
    languageList: [],
    // 语言列表
    pagination: {} // 分页信息

  },
  effects: {
    // 查询数据
    fetchLanguages: /*#__PURE__*/_regeneratorRuntime.mark(function fetchLanguages(_ref, _ref2) {
      var payload, call, put, page, query, response, languageData;
      return _regeneratorRuntime.wrap(function fetchLanguages$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              page = payload.page, query = _objectWithoutProperties(payload, _excluded);
              _context.next = 5;
              return call(queryLanguage, payload);

            case 5:
              response = _context.sent;
              languageData = getResponse(response);

              if (!languageData) {
                _context.next = 10;
                break;
              }

              _context.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  query: query,
                  pagination: createPagination(languageData),
                  languageList: languageData.content
                }
              });

            case 10:
            case "end":
              return _context.stop();
          }
        }
      }, fetchLanguages);
    }),
    // 编辑数据
    editLanguage: /*#__PURE__*/_regeneratorRuntime.mark(function editLanguage(_ref3, _ref4) {
      var payload, call, response;
      return _regeneratorRuntime.wrap(function editLanguage$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call;
              _context2.next = 4;
              return call(_editLanguage, payload);

            case 4:
              response = _context2.sent;
              return _context2.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context2.stop();
          }
        }
      }, editLanguage);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref5) {
      var payload = _ref5.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};